package com.zhike.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhike.domain.DwsTripBtWindow;
import com.zhike.service.DwsTripBtWindowService;
import com.zhike.mapper.DwsTripBtWindowMapper;
import com.zhike.utils.PieUtils;
import com.zhike.utils.Result;
import com.zhike.utils.ResultCodeEnum;
import com.zhike.utils.BarUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author ASUS
* @description 针对表【dws_trip_bt_window(车型品牌行驶表)】的数据库操作Service实现
* @createDate 2024-05-01 15:26:20
*/
@Service
public class DwsTripBtWindowServiceImpl extends ServiceImpl<DwsTripBtWindowMapper, DwsTripBtWindow>
    implements DwsTripBtWindowService{
    @Autowired
    private DwsTripBtWindowMapper dwsTripBtWindowMapper;
    @Override
    public String getAvgTypeToday(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgCountTypeToday = dwsTripBtWindowMapper.getAvgTypeToday(date);
            Map data = BarUtils.getDetailSingle(avgCountTypeToday,"car_type","avg_speed","平均速度");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getCountTypeToday(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgCountTypeToday = dwsTripBtWindowMapper.getCountTypeToday(date);
            List<Map> maps = PieUtils.getDeatail(avgCountTypeToday,"car_type","danger_count");
            return Result.ok(JSON.toJSONString(maps)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getAvgBrandToday(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgCountTypeToday = dwsTripBtWindowMapper.getAvgBrandToday(date);
            Map data = BarUtils.getDetailSingle(avgCountTypeToday,"brand","avg_speed","平均速度");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getCountBrandToday(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgCountTypeToday = dwsTripBtWindowMapper.getCountBrandToday(date);
            List<Map> maps = PieUtils.getDeatail(avgCountTypeToday,"brand","danger_count");
            return Result.ok(JSON.toJSONString(maps)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getAvgTypeWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgCountTypeToday = dwsTripBtWindowMapper.getAvgTypeWeek(date);
            Map data = BarUtils.getDetailSingle(avgCountTypeToday, "car_type", "avg_speed", "平均速度");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getCountTypeWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgCountTypeToday = dwsTripBtWindowMapper.getCountTypeWeek(date);
            List<Map> maps = PieUtils.getDeatail(avgCountTypeToday,"car_type","danger_count");
            return Result.ok(JSON.toJSONString(maps)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getAvgBrandWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgCountTypeToday = dwsTripBtWindowMapper.getAvgBrandWeek(date);
            Map data = BarUtils.getDetailSingle(avgCountTypeToday,"brand","avg_speed","平均速度");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getCountBrandWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgCountTypeToday = dwsTripBtWindowMapper.getCountBrandWeek(date);
            List<Map> maps = PieUtils.getDeatail(avgCountTypeToday,"brand","danger_count");
            return Result.ok(JSON.toJSONString(maps)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }


}




